package gold.digger;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC846 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean isNStraightHand(int[] hand, int groupSize) {
            if (hand.length <= 0 || hand.length % groupSize != 0) return false;
            TreeMap<Integer, Integer> handMap = new TreeMap<>();
            for (int num : hand) {
                handMap.put(num, handMap.getOrDefault(num, 0) + 1);
            }
            for (Integer num : handMap.keySet()) {
                if (handMap.get(num) <= 0) continue;
                for (int j = 0; j < handMap.get(num); j++) {
                    for (int i = 1; i < groupSize; i++) {
                        if (handMap.getOrDefault(num + i, 0) <= 0) return false;
                        else handMap.put(num + i, handMap.get(num + i) - 1);
                    }
                }
            }

            return true;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,1,2,2,3,3]");
        System.out.println(solution.isNStraightHand(arr, 3));
    }

    public static void main(String[] args) throws Exception {
        LC846 an = new LC846();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
